package com.mlz.mlzmall.ui.activity.act.order;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.mlz.mlzmall.R;
import com.mlz.mlzmall.bean.ResponseLogistics;
import com.mlz.mlzmall.bean.ResponseOrderInfo;
import com.mlz.mlzmall.ui.activity.act.address.postCodeToPCD;
import com.mlz.mlzmall.ui.activity.act.base.TopTitleView;
import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.VolleyBaseActivity;
import com.mlz.mlzmall.ui.adapter.TrackingForLogisticAdapter;
import com.mlz.mlzmall.ui.utils.mlz.ConfigInfo;
import com.mlz.mlzmall.ui.utils.mlz.URL;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;

/**
 * Created by jack.wang on 2016/1/19.
 */
public class LogitsticsCheck extends VolleyBaseActivity implements View.OnClickListener {
    // 收货人姓名
    TextView tvNameForLogistics;
    // 收货人电话
    TextView tvPhoneForLogistics;
    // 收货人地址
    TextView tvDetailAddressForLogistics;
    // 订单号
    TextView tvOrderForLogistics;
    // 快递公司名称
    TextView shippingCompany;
    // 快递单号
    TextView shippingNumber;
    // 物流跟踪信息ListView
    ListView listViewLogistics;
    // 布局
    int resource;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.check_logistics_activtiy);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        // 获取订单号
        String orderCode = this.getIntent().getStringExtra("orderCode").toString();
        //初始化控件
        initView();
        // 访问网络更新UI
        loadData(orderCode);
        setViewContent();
    }
    private void setViewContent() {
        TopTitleView loginTopTitle = (TopTitleView) findViewById(R.id.login_top_title);
        loginTopTitle.setLeftButtonContent(null, R.mipmap.pic_2_1_btn_back, null);
        loginTopTitle.setTitleButtonContent("查看物流", null, null);
        loginTopTitle.setOnLeftButtonClickListener(new TopTitleView.OnLeftButtonClickListener() {
            @Override
            public void onLeftBtnClick(View button) {
                finish();
                LogitsticsCheck.this.overridePendingTransition(R.anim.right_in,R.anim.right_out);
            }
        });
    }

    private void initView() {
        tvNameForLogistics = (TextView) findViewById(R.id.tvNameForLogistics);
        tvPhoneForLogistics = (TextView) findViewById(R.id.tvPhoneForLogistics);
        tvDetailAddressForLogistics = (TextView) findViewById(R.id.tvDetailAddressForLogistics);
        tvOrderForLogistics = (TextView) findViewById(R.id.tvOrderForLogistics);
        shippingCompany = (TextView) findViewById(R.id.shippingCompany);
        shippingNumber = (TextView) findViewById(R.id.shippingNumber);
        listViewLogistics = (ListView) findViewById(R.id.listViewLogistics);
        resource = R.layout.check_logistics_item;
    }

    private void loadData(final String orderCode) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    // 创建一个restTemplate 对象
                    RestTemplate restTemplate = new RestTemplate();
                    HttpHeaders httpHeaders = new HttpHeaders();
                    String sessionKeyLogin = sessionId;
                    // 设置请求头
                    httpHeaders.set("Cookie", "JSESSIONID=" + sessionKeyLogin);
                    // 查看物流地址信息
                    String OrderInfoUrl = URL.ORDER.DETAIL + "?orderCode=" + orderCode;
                    // 物流跟踪信息
                    String trackingUrl = URL.ORDER.ROUTE + "?orderCode=" + orderCode;
                    // 把请求头放在请求实体里
                    httpHeaders.setContentType(new MediaType("application", "json"));
                    HttpEntity<?> requestEntity = new HttpEntity<Object>(httpHeaders);
                    final ResponseEntity<ResponseOrderInfo> OrderInfo = restTemplate.exchange(OrderInfoUrl, HttpMethod.GET, requestEntity, ResponseOrderInfo.class);

                    ResponseEntity<ResponseLogistics> trackingForLogistic = restTemplate.exchange(trackingUrl, HttpMethod.GET, requestEntity, ResponseLogistics.class);
                    final ArrayList<ResponseLogistics.Data> data = new ArrayList<ResponseLogistics.Data>();
                    // 添加每条物流信息
                    if(null != trackingForLogistic.getBody().getData()){
                        for (int i = 0; i < trackingForLogistic.getBody().getData().size(); i++) {
                             // 第一条数据做标记
                             if(i==0){
                                 trackingForLogistic.getBody().getData().get(i).setIsfirst(true);
                             }else{
                                 trackingForLogistic.getBody().getData().get(i).setIsfirst(false);
                             }
                            data.add(trackingForLogistic.getBody().getData().get(i));
                        }
                    }

                    //  更新Ui
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            tvNameForLogistics.setText(OrderInfo.getBody().getData().getShippingFullname());
                            tvPhoneForLogistics.setText(OrderInfo.getBody().getData().getShippingTelephone());
                            // 获取省-市-区-详细地址
                            StringBuffer sb = new StringBuffer();
                            String RegionCode = String.valueOf(OrderInfo.getBody().getData().getShippingRegionCode());
                            postCodeToPCD postCodeToPCD = new postCodeToPCD(LogitsticsCheck.this);
                            sb.append(postCodeToPCD.province(RegionCode));
                            sb.append(postCodeToPCD.city(RegionCode));
                            sb.append(postCodeToPCD.district(RegionCode));
                            sb.append(OrderInfo.getBody().getData().getShippingAddress());
                            tvDetailAddressForLogistics.setText(sb);
                            tvOrderForLogistics.setText(orderCode);
                            shippingCompany.setText(OrderInfo.getBody().getData().getShippingCompany());
                            shippingNumber.setText(OrderInfo.getBody().getData().getShippingNumber());
                            // 物流跟踪信息
                            TrackingForLogisticAdapter trackingAdapter = new TrackingForLogisticAdapter(getBaseContext(), resource, data);
                            listViewLogistics.setAdapter(trackingAdapter);
                        }
                    });


                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

}
